//
//  UIAlterCenterView.m
//  YLLottery
//
//  Created by apple on 2017/9/30.
//  Copyright © 2017年 Apple. All rights reserved.
//

#import "UIAlterCenterView.h"
#define UIAlterCenterView_Width_Margin 8
#define UIAlterCenterView_Height_Margin 8

#define UIAlterCenterView_maxWidth   270*get6sConstantWidthScale()

@interface UIAlterCenterView()

@property (nonatomic, strong) UILabel *textLabel;

@end

static  UIAlterCenterView *_alterCenterView;
@implementation UIAlterCenterView
- (instancetype)initWithFrame:(CGRect)frame
{
    if(self = [super initWithFrame:frame])
    {
        [self addSubview:self.textLabel];
        self.backgroundColor = [UIColor blackColor];
        self.layer.cornerRadius = 10;
        self.layer.masksToBounds = YES;
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    WS(weakSelf);
    [self.textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(weakSelf);
    }];
    
}


#pragma mark - API
+ (void)showTimeString:(NSString *)text
{
    CGPoint center = CGPointMake(getScreenWidth() * 0.5, getScreenHeight() *0.5);
    [self showWithString:text center:center afterDimiss:YES];
    
}
+ (void)showAlwaysString:(NSString *)text
{
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    CGPoint center = keyWindow.center;
    [self showWithString:text center:center afterDimiss:NO];
}

+ (void)showWithString:(NSString *)text center:(CGPoint)center  afterDimiss:(BOOL)isdismiss
{
    //7.0不支持这个方法
    if([[UIDevice currentDevice].systemVersion floatValue] < 8.0)
        return;
    
    UIView *superView = [UIApplication sharedApplication].keyWindow;
    if(_alterCenterView)
    {
        if([_alterCenterView.textLabel.text isEqualToString: text])
            return;
    }
    else
        _alterCenterView = [[self alloc]init];
    
    
    [superView addSubview:_alterCenterView];
    
    
    _alterCenterView.textLabel.text = text;
    CGSize textSize = [text boundingRectWithSize:CGSizeMake(UIAlterCenterView_maxWidth, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15.0]} context:nil].size;
    
    CGFloat width = ceilf(textSize.width) + 2*UIAlterCenterView_Width_Margin;
    CGFloat height = ceilf(textSize.height) +2*UIAlterCenterView_Height_Margin;
    _alterCenterView.bounds = CGRectMake(0, 0, width, height);
    _alterCenterView.center = center;
    
    
    //设置内部控件
    [_alterCenterView shakeToShow:_alterCenterView];
    
    //默认情况
    if(!isdismiss)  return;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [_alterCenterView cancleAlterView];
    });
}


+ (void)dissmissAlterView
{
    if(_alterCenterView != nil)
        [_alterCenterView cancleAlterView];
}

- (void)cancleAlterView
{
    WS(weakSelf);
    weakSelf.alpha = 1.0;
    [UIView animateWithDuration:0.25 animations:^{
        weakSelf.alpha = 0.0;
        weakSelf.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.0];
    } completion:^(BOOL finished) {
        [_alterCenterView removeFromSuperview];
        _alterCenterView = nil;
        [weakSelf removeFromSuperview];
    }];
}





//弹出放大-缩小效果
- (void) shakeToShow:(UIView*)aView
{
    CAKeyframeAnimation* animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    animation.duration = 0.5;
    NSMutableArray *values = [NSMutableArray array];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.2, 1.2, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.8, 0.8, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
    animation.values = values;
    [aView.layer addAnimation:animation forKey:nil];
}

- (UILabel *)textLabel
{
    if(_textLabel == nil)
    {
        _textLabel = [UILabel new];
        _textLabel.font = [UIFont systemFontOfSize:14.0];
        _textLabel.textColor = [UIColor whiteColor];
        _textLabel.textAlignment = NSTextAlignmentCenter;
        _textLabel.numberOfLines = 0;
    }
    return _textLabel;
}


@end
